<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/15 0015
 * Time: 17:09
 */

namespace app\api\controller\efficiency;

/*
 * 效率花呗接口类
 * */

use think\Request;

class Efficiency extends Base
{
    private $Sign;
    private $time;
    private $type = [
        1=>'applyForLoans',
        2=>'applyForResult',
        3=>'refundResult'
    ];
    private $loanID = 0;
    private $LoansData = [];
    private $data = [];
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->Sign = new Sign();
        $this->time = time();
    }
    /*
     * 控制方法调用
     * */
    public function totle($loanID,$Type){
//        验证参数
        if ($loanID <= 0 || !isset($this->type[$Type])) return ['code'=>0,'info'=>'参数错误'];
        $this->loanID = $loanID;
//        查询相应数据
        $res = $this->SelectData();
        if ($res['code'] == 0) return $res;
//        调用接口
        $way = $this->type[$Type];
        return $this->$way();
    }
    /*
     *数据查询与组合
     * */
    private function SelectData(){
        $this->LoansData = model('LoansApplylist')->SelectData(['ID'=>$this->loanID]);
        if (empty($this->LoansData)) return ['code'=>0,'info'=>'订单数据未找到'];
        $Mobile = model('MemInfo')->SelectData(['ID'=>$this->LoansData['UserID']],'Mobile');
        $this->data = [
            'request_time'=>$this->time,
            'phone'=>$Mobile['Mobile'],
            'loan_sn'=>$this->LoansData['OrderSn']
        ];
        return ['code'=>200];
    }
    /*
     * 用户注册状态
     * */
//    public function registerStatus($mobile = ''){
//        $this->data['reg_time'] = $this->time;
//        $data = $this->Sign->SignEncrypt($this->data,1);
//        $url = $this->Url.$this->UrlOperation[1];
//        $res = $this->request_post($url,$data);
//        p(json_decode($res,true));die;
//    }
    /*
     * 用户申请贷款
     * */
    private function applyForLoans(){
        $this->data['apply_time'] = strtotime($this->LoansData['ApplyTime']);
        return $this->executeApi(2);
    }
    /*
     * 用户申贷结果
     * */
    private function applyForResult(){
        $this->data['review_time'] = strtotime($this->LoansData['ShTime']);
        $this->data['review_status'] = $this->LoansData['LoanStatus'] == 2?20:10;
        return $this->executeApi(3);
    }
    /*
     * 用户还款结果
     * */
    private function refundResult(){
        $this->data['repayment_time'] = strtotime($this->LoansData['SjFkTime']);
        $status = 10;
        if ($this->LoansData['LoanStatus'] == 3){
            if ($this->LoansData['IsYQ'] > 0) {
                $status = 20;
            }
        }else{
            $status = 20;
        }
        $this->data['repayment_status'] = $status;
        return $this->executeApi(4);
    }
    /*
     * 执行API
     * */
    private function executeApi($type){
        $data = $this->Sign->SignEncrypt($this->data,$type);
        $url = $this->Url.$this->UrlOperation[$type];
        $res = $this->request_post($url,$data);
        return $this->Json_Info($res);
    }
    /*
     * 数据返回
     * */
    private function Json_Info($info){
        $infoArr = json_decode($info,true);
        if($infoArr['error'] == true){
            return ['code'=>0,'info'=>$infoArr['info']];
        }else{
            return ['code'=>200,'info'=>$infoArr['info']];
        }
    }
}